﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using ALiteTeam.PackageTracer.Data;
using System.Threading;


namespace ALiteTeam.PackageTracer.Core.Impl
{
    /// <summary>
    /// 单例模式的DeliveryData操作类，用于保障Xml数据只加载一次
    /// </summary>
    public sealed class DeliveryDataOp : IDeliveryOperate
    {
        private DeliveryDataOp()
        {
            LogWritter = new GeneralLogWriter();
        }

        public ILogWrite LogWritter { get; set; }

        private static readonly DeliveryDataOp instance;
        static DeliveryDataOp()
        {
            instance = new DeliveryDataOp();
        }

        public static DeliveryDataOp Instance
        {
            get
            {
                return instance;
            }
        }

        private DeliveriesContainer _DeliveryResource;
        public DeliveriesContainer DeliveryResource
        {
            get
            {
                if (_DeliveryResource == null)
                {
                    _DeliveryResource = LoadDeliveryResource();
                }

                return _DeliveryResource;
            }
            private set
            {
                _DeliveryResource = value;
            }
        }

        /// <summary>
        /// 从XML加载的快递商信息
        /// </summary>
        private DeliveriesContainer LoadDeliveryResource()
        {
            try
            {
                var resxStream = Application.GetResourceStream(new Uri("/ALiteTeam.PackageTracer.Core;component/ExpressDeliveryInfo.xml", UriKind.Relative));
                XmlSerializer serializer = new XmlSerializer(typeof(DeliveriesContainer));

                DeliveryResource = serializer.Deserialize(resxStream.Stream) as DeliveriesContainer;
            }
            catch (Exception ex)
            {
                LogWritter.Write(ex);
            }

            return DeliveryResource;
        }

        public IEnumerable<PublicGrouping<string, Delivery>> GetDeliveryGroups()
        {
            IEnumerable<PublicGrouping<string, Delivery>> result = null;
            if (DeliveryResource != null)
            {
                result = from delivery in this.DeliveryResource.Deliveries
                             group delivery by delivery.Prefix into d
                             orderby d.Key
                             select new PublicGrouping<string, Delivery>(d);
            }

            return result;
        }

        public IEnumerable<Delivery> GetPriorityDeliveries(int topNumber)
        {
            IEnumerable<Delivery> result = null;
            result = this.DeliveryResource.Deliveries.OrderBy(e => e.Priority).Take(topNumber);

            return result;
        }
    }
}
